<template>
    <div>
        <t-header></t-header>
        <div class="video-wrap w">
            <el-select v-model="currentVideoCat" placeholder="请先完成视频分类的选择">
                <el-option
                    v-for="item in videoCat"
                    :key="item"
                    :label="item"
                    :value="item">
                </el-option>
            </el-select>
            <el-input v-model="videoName" placeholder="请输入视频名称后上传"></el-input>
            <div class="upload" v-if="currentVideoCat != '' && videoName != ''">
                <input @change="uploadVideo" title="请选择上传的视频" type="file">
                <div class="upload-box">点击上传视频</div>
            </div>
        </div>
    </div>
</template>

<script>
import Header from '../../components/global/Header';
import axios from 'axios';
import { onMounted, ref } from '@vue/composition-api';
import jquery from 'jquery';
export default {
    components: {
        't-header': Header
    },
    setup(props, context) {
        axios.defaults.baseURL = 'http://47.105.157.158:3000';

        onMounted(() => {
            getVideoCat();
        })

        // 视频分类
        const videoCat = ref('');
        // 视频名称
        const videoName = ref('');
        // 当前所选中的视频分类
        const currentVideoCat = ref('');
        // 获取视频分类
        const getVideoCat = async function () {
            let { data: res } = await axios.get('/video/video/cat');
            if (res.status != 200)
                return context.parent.$message.error('获取视频分类失败，请刷新后重试');
            videoCat.value = res.cat;
        }

        // 上传视频
        const uploadVideo = async function (e) {
            let formData = new FormData();
            formData.append('video', e.target.files[0]);

            // 获取发布的用户邮件
            let { data: res } = await axios.get(`/user/login/${window.localStorage.getItem('token')}`);
            if (res.status != 200)
                return context.parent.$message.error('发布视频失败，请重新登录后尝试');

            jquery.ajax({
                type: 'post',
                url: `http://47.105.157.158:3000/video/video/${currentVideoCat.value}/${videoName.value}/${res.user.email}`,
                data: formData,
                contentType: false,
                processData: false,
                success: function (response) {
                    if (response.status == 200) {
                        context.parent.$message.success(response.msg);
                        setTimeout(function () {
                            location.reload();
                        }, 1600)
                    } else {
                        context.parent.$message.error('上传视频失败，遇到未知错误');
                    }
                }
            });
        }


        const methods = { uploadVideo }

        return {
            ...methods,
            videoCat,
            currentVideoCat,
            videoName
        }
    }
}
</script>

<style lang="less" scoped>
.video-wrap {
    padding-top: 60px;
    text-align: center;
}

.upload {
    position: relative;
    width: 300px;
    height: 100%;
    margin: 20px auto 0;
    input {
        width: 300px;
        height: 50px;   
        border: 1px solid #ccc;
        opacity: 0;
        cursor: pointer;
    }
    .upload-box {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        line-height: 50px;
        text-align: center;
        background-color: #3ea0f1;
        border-radius: 12px;
        color: #fff;
        font-weight: 700;
        cursor: pointer;
        pointer-events: none;
    }
}

.el-input {
    width: 300px;
}
</style>